package Lee_code;

public class Lee_200_dfs {
    int row, column;
    int count=0;

    public int numIslands(char[][] grid) {
        row = grid.length;
        column = grid[0].length;

        for(int i=0; i<row; i++)
        {
            for (int j=0; j<column; j++)
            {
                if(grid[i][j]=='1')
                {
                    count++;
                    dfs(grid, i, j);
                }
            }
        }
        return count;
    }
    public void dfs(char[][] grid, int temp_row, int temp_column)
    {
        if(temp_row>=row || temp_column>=column || temp_column<0 || temp_row<0|| grid[temp_row][temp_column]=='0')
        {
            return;
        }
        else if (grid[temp_row][temp_column]=='1')
        {
            //置为0
            grid[temp_row][temp_column]='0';
            //上
            dfs(grid, temp_row+1, temp_column);
            //下
            dfs(grid, temp_row-1, temp_column);
            //右
            dfs(grid, temp_row, temp_column+1);
            //左
            dfs(grid, temp_row, temp_column-1);
        }
    }
}
